function parseNum(val, scale) {
		try{
			if(val.indexOf(',') >=0)val = val.replace(/,/g,"");
		}catch(e){}
		    
		    if (isNaN(val) == true || val == undefined || val == '') {
		        return 0;
		    }
		    // 不设的话默认不带小数
		    if (scale == null || scale == undefined) {
		        scale = 2;
		    }
		    val = parseFloatExtension(val);

		    return parseFloatExtension(Math.round(val * Math.pow(10,scale)) / Math.pow(10,scale));
		}

function parseFloatExtension(val){
    return Math.round(parseFloat(val) * Math.pow(10,12))/Math.pow(10,12);
}


        function exportExcel (){
            var title = $("#calc_title").html();
            
            var table_datas  = [ ];
            table_datas.push(toHtmlData($("#sample_table_header" ),'标本量信息', true  ));

            var sample_table_sum = toHtmlData($("#sample_table_sum" ),null , false  );
            sample_table_sum.fields[0].colspan = 9;
            table_datas.push(sample_table_sum);

            table_datas.push(toHtmlData($("#sample_table") ,null, false  ));
            var sumInfo_table = toHtmlData($("#sumInfo_table" ),'收益汇总', false  );
            sumInfo_table.fields[1].colspan = 3 ; 
            sumInfo_table.fields[3].colspan = 2 ; 

            table_datas.push(sumInfo_table);
            table_datas.push(toHtmlData($("#sum_table" ),null, true  ));
            table_datas.push(toHtmlData($("#profit_table" ),'测算收益', true  ));

            var talbleht = toHtml(table_datas);//console.log(talbleht);

            toExcel(talbleht,title+".xls");
        }

        //计算收益结果 
		/*
			data 销售及成本
			copY 合作年限
			others 其他收入
			equip 仪器成本 
			additional 增值服务
			feeRito 销售费用比例
			accDate 客户帐期
			developFee 开发费用
			
		*/
		function calcResult(data , copY, others,equip,additional  , accDate,developFee,type  ){

			var resdata = {
				sum:[],
				data:[],
			}
			var res  = [];
			var sum = {
				otherincome:0,
				equipcost:0,
				addtax:0,
				motionfee:0,
				moneycost:0,
				repaircost:0,
				servicefee:0,
				incometax:0,
				curprofit:0,
				curmoney:0,

			};

			var eqSum = 0 ;
			var sum5 = {
				equipcost:0,
				regentsale:0,
				curprofit:0,
				profitrito:0,
				backday:0,
				mkrito:0,

			};
			var equipcosts = [];
			//增值服务成本
			var addsum = 0 ; 
			for(var i = 0 ; i < additional.length ; i ++ ){
				addsum += parseNum(additional[i].cost);
			} 
			for(var i = 0 ; i < copY ; i ++ ){
				//无税仪器成本
				equipcosts.push(i == 0 ? (equip[equip.length -1 ].sumcost + addsum) / (1+ CONFIG.TAXRITO ) / 10000 : 0 ) ;

				eqSum += equipcosts[i] ;
				if(copY > 5 && i < 5 ){
					sum5.equipcost += equipcosts[i] ;
				}
			}
			//现金流计算
			for(var i = 0 ; i < copY ; i ++){
				//仪器成本*Math.pow(1.1, -(year - 1))
				 	if(i > 0){
				 		sum.curmoney += equipcosts[i] *Math.pow(1+CONFIG.NPVRITO,-i)  ;
				 	}else{
				 		sum.curmoney += equipcosts[i] ;
				 	}
			}

			var curmoneys = new Array();
			var curmoneys5 = new Array();

			///销售费用比例
			var feeRito = data.data[0].feeRito / 100 ;

			for(var i = 0 ; i <= copY ; i ++){
				var d = {};

				d.regentsale = data.sum["notaxsale"+ i] / 10000;//试剂销售
				d.regentcost = data.sum["notaxcost"+ i] / 10000;//试剂成本
				d.taxregentsale = data.sum["sale"+ i] / 10000;
				d.mksale = data.sum["mksale"+ i] / 10000;//迈克试剂销售
				d.nomksale = data.sum["nomksale"+ i] / 10000;//非迈克试剂销售

				if(i != copY){

					d.year = "第"+(i+1)+"年";

					//其他收入
					d.otherincome = others[i] /(1+ CONFIG.TAXRITO ) / 10000;
					sum.otherincome += d.otherincome;

					//仪器成本
					d.equipcost = equipcosts[i] ;
					sum.equipcost += d.equipcost;
					
					//增值税
					d.addtax = (d.regentsale + d.otherincome - d.equipcost - d.regentcost) * CONFIG.TAXRITO 
					sum.addtax += d.addtax;

					//运营费用
					d.motionfee = d.regentsale * CONFIG.servicefeeRito ;
					sum.motionfee += d.motionfee ;

					//资金成本
					d.moneycost =  type == CONFIG.sub ? (d.mksale  * accDate / 12 * CONFIG.moneycostRito +  d.nomksale  * accDate / 12 * CONFIG.notmkMoneycostRito ) : type == CONFIG.group ? d.taxregentsale * CONFIG.moneycostRito * accDate / 12 :0 ;  
					sum.moneycost += d.moneycost;

					//维修成本
					d.repaircost = equip.length > 0 ? equip[equip.length - 1 ].sumrepaircost / 10000 : 0;
					sum.repaircost += d.repaircost; 

					//业务费用
					d.servicefee = d.regentsale  * feeRito   / CONFIG.servicecostRito;
					
					if(i == 0 ) d.servicefee += developFee / CONFIG.servicecostRito / 10000  //第一年 加 开发费用
					sum.servicefee += d.servicefee;

					//所得税
					if(type == CONFIG.sub ){

						//d.incometax =( d.regentsale + d.otherincome -d.regentcost- equipcosts[0] / 5  - d.motionfee - d.moneycost - d.repaircost - d.servicefee  - d.addtax *CONFIG.addtaxattachRito ) * CONFIG.incometaxRito;
						d.incometax =( d.regentsale + d.otherincome -d.regentcost- equipcosts[0] / copY - d.motionfee - d.moneycost - d.repaircost - d.servicefee  - d.addtax *CONFIG.addtaxattachRito ) * CONFIG.incometaxRito;

					}else if(type == CONFIG.group){

					//	d.incometax = ( d.regentsale + d.otherincome -d.regentcost- equipcosts[0] / 5  - d.motionfee - d.moneycost - d.repaircost - d.servicefee  - d.addtax *CONFIG.addtaxattachRito ) * (CONFIG.incometaxRito * (1- data.rito / 100 )  + CONFIG.mkincometaxRito  * data.rito/ 100 ) ;
						d.incometax = ( d.regentsale + d.otherincome -d.regentcost- equipcosts[0] / copY  - d.motionfee - d.moneycost - d.repaircost - d.servicefee  - d.addtax *CONFIG.addtaxattachRito ) * (CONFIG.incometaxRito * (1- data.rito / 100 )  + CONFIG.mkincometaxRito  * data.rito/ 100 ) ;
					}
				 	

				 	sum.incometax += d.incometax; 

				 	//当年利润
				 	//d.curprofit = d.regentsale + d.otherincome-d.regentcost -   equipcosts[0] / 5 - d.motionfee - d.moneycost - d.repaircost - d.servicefee  - d.addtax * CONFIG.addtaxattachRito - d.incometax ;
				 	d.curprofit = d.regentsale + d.otherincome-d.regentcost -   equipcosts[0] / copY - d.motionfee - d.moneycost - d.repaircost - d.servicefee  - d.addtax * CONFIG.addtaxattachRito - d.incometax ;

				 	sum.curprofit += d.curprofit;

				 	//计算 以5年 标准
				 	if(copY > 5 && i < 5 ){
				 		sum5.regentsale += d.regentsale;
				 		sum5.curprofit += d.curprofit;

				 	}
				 	
				}else{
					d.year = "合计";
					d.otherincome = sum.otherincome ;
					d.equipcost = sum.equipcost;
					d.addtax = sum.addtax;
					d.motionfee = sum.motionfee;
					d.moneycost = sum.moneycost;
					d.repaircost = sum.repaircost;
					d.servicefee = sum.servicefee;
					d.incometax = sum.incometax;
					d.curprofit = sum.curprofit;


				}
				//当年现金流 = 仪器成本 + 当年利润
				if(i == 0) {

					d.curmoney = - sum.curmoney * (1+ CONFIG.TAXRITO );

				}else{

					d.curmoney = (eqSum/copY* (1+ CONFIG.TAXRITO ) + res[i -1].curprofit);
					//d.curmoney = (eqSum/5* (1+ CONFIG.TAXRITO ) + res[i -1].curprofit);
				}

				curmoneys.push(d.curmoney);

				if(copY > 5 && i < 5 )curmoneys5.push(d.curmoney);
				
				res.push(d);

			}

			//收益率
			res[0].profitrito = IRR(curmoneys) * 100;

			if(copY > 5 )sum5.profitrito = IRR(curmoneys5) * 100;

			//回收期
			var mons = copY * 12 ; 
			var R = res[copY].equipcost / ( res[copY].equipcost +res[copY].curprofit ); 
			res[0].backday = calcBackDay(copY,R,mons);

			if(copY > 5){
				R = sum5.equipcost / (sum5.equipcost + sum5.curprofit );
				mons = 60 ; 
				sum5.backday = calcBackDay(5,R,mons);

			}


			resdata.data = res;
			if(copY > 5){
				resdata.sum.push({
					copY:  "5年参考",
					sale: sum5.regentsale,
					cost: sum5.equipcost,
					profit: sum5.curprofit,
					rito:sum5.profitrito,
					backday:sum5.backday,
					mkrito:data.rito,

				});
			}
			resdata.sum.push({
				copY: "本次"+copY + "年",
				sale: res[copY].regentsale,
				cost: res[copY].equipcost,
				profit: res[copY].curprofit,
				rito:res[0].profitrito,
				backday:res[0].backday,
				mkrito:data.rito,

			});

			
			return resdata; 

		}

		
		function calcBackDay(copY, R , mons ){
			mons = 60;
			return ( R * copY < 0 ) ? Math.round( ( R  *mons  - Math.floor( R  * mons )   ) * 12,0) +"个月" :  Math.floor( R*mons/12 ) +"年" +  Math.round( ( R  *mons  - Math.floor( R  * mons )   ) * 12,0) +"个月" ; 
		}
		////计算销售额及成本
    	function calcSaleAndCost(data,copY,incrRito,feeRito,type  ){
	
			var res = {

				data :[],
				sum :{},
				rito:0,
			}
			for(var i = 0 ; i <= copY ;  i ++){
				//销售额合计 按年度
				res.sum['sale'+i] = 0 ; 
				res.sum['notaxsale'+i] = 0 ;

				res.sum['mksale'+i] = 0 ;
				res.sum['nomksale'+i] = 0 ;

				//样本量合计 按年度
				res.sum['sample'+i] = 0 ; 
				//样成本合计 按年度
				res.sum['cost'+i] = 0 ; 
				res.sum['notaxcost'+i] = 0 ;




			}
			var mksale = 0;var sumsale= 0 ; 

			var maccuraBrand = getMaccuraBrand();
			for(var i = 0 ; i < data.length ; i++){


				var d = data[i];
				//序号
				d.index = (i + 1);

				//销售费用比例
				d.feeRito  = feeRito ;
				//客户折扣
				d.discount = parseNum(d.unitPrice) == 0 ? 0 : d.salesPrice / d.unitPrice ;
				//客户每测试价
				d.perprice = parseNum(d.testNum) == 0 ? 0 :  d.salesPrice / d.testNum ;  
		

    			
    			//每包装成本(含税) d.cost 
    			d.cost =  type ==  CONFIG.sub ? d.subcost : type == CONFIG.group ? d.groupcost : 0 ;
    			//子公司折扣
    			d.subdiscount = parseNum(d.unitPrice) == 0 ? 0 :  d.cost / d.unitPrice ; 
    			//毛利率
    			d.gross = parseNum(d.salesPrice) == 0 ? 0 : (1 -  d.cost / d.salesPrice ) * 100 ; 
    			//每测试成本
    			d.percost = parseNum(d.testNum) == 0 ? 0 : d.cost / d.testNum ;

				//销售额合计 按产品
				d['sale'+copY] = 0 ; 
				//样本量合计 按产品
				d['sample'+copY] = 0 ; 
				//成本合计 按产品
				d['cost'+copY] = 0 ;


				
				var isMk = d.brand != null && maccuraBrand[d.brand] ; 
				

			    for(var j = 0 ; j < copY ; j++){

					if( j == 0){
						//销售额
						d["sale"+j] = d["sample"+j]  * d.perprice ;  
						
					}else{
						//样本量
						d["sample"+j] = d["sample"+(j-1)] * (1+ incrRito[j]/ 100 ) ;
						
						
					}
					//销售额
					d["sale"+j] = CONFIG.isSpecialProduct(d.serialNo) ? CONFIG.specialPrice(d["sample"+j]):  d["sample"+j] * d.perprice ; 

					

					//无税销售额
    				d["notaxsale"+j] =  d["sale"+j] / (1 + parseNum(d.tax) / 100 ) ; 

    				if(isMk){

						mksale+= d["sale"+j];
						d["mksale"+j]  = d["sale"+j] ;
						d["nomksale"+j]  = 0 ;
					}else{
						d["nomksale"+j]  = d["sale"+j] ;
						d["mksale"+j] = 0 ;
					}

					sumsale+= d["sale"+j];

					//成本
					d["cost"+j] = CONFIG.isSpecialProduct(d.serialNo) ? CONFIG.specialCost(d["sale"+j], type ): d["sample"+j] * d.percost ;
					//无税成本
    				d["notaxcost"+j] =  d["cost"+j] / (1 + parseNum(d.tax) / 100 ) ; 

					//console.log(d.serialNo +"=> "+CONFIG.isSpecialProduct(d.serialNo)+"==>  " +d["sample"+j]+" * " +d.percost +" = "+ ( d["sample"+j] * d.percost));
					//按产品 合计
					d['sale'+copY] += d["sale"+j] ;
					d['sample'+copY] += d["sample"+j] ; 
					d['cost' +copY] += d["cost"+j];
					




					//按年 合计
					res.sum['sale'+j]+= d["sale"+j];
					res.sum['sample'+j] += d["sample" + j ];
					res.sum['cost'+j]+= d["cost"+j];
						//无税
						res.sum['notaxsale'+j] += d["notaxsale"+j]; 
						res.sum['notaxcost'+j]+= d["notaxcost"+j];

						res.sum['mksale'+j]+= d['mksale'+j];
						res.sum['nomksale'+j]+= d['nomksale'+j];

					//总 合计
					res.sum['sale'+copY] += d["sale"+j];
					res.sum['sample'+copY] += d["sample"+j];
					res.sum['cost'+copY]+= d["cost"+j];
						//无税
						res.sum['notaxsale'+copY] += d["notaxsale"+j]; 
						res.sum['notaxcost'+copY]+= d["notaxcost"+j];
						res.sum['mksale'+copY]+= d['mksale'+j];
						res.sum['nomksale'+copY]+= d['nomksale'+j];
				}


				res.data.push(d);


			}
			res.rito = mksale / sumsale * 100 ;
		    return  res ; 
		}


		function calcEquipCost(data, type ){

			var res = [];
			var sum = {
							productName:'测算仪器成本合计(元)',
							cost:0,
							repaircost: 0 ,
							num: 0 ,
							sumcost: 0 , 
							sumrepaircost: 0 
					  };
			for(var i = 0 ; i < data.length ; i ++){
				var d = data[i];
				d.cost = type == CONFIG.sub ? d.subcost : type == CONFIG.group ? d.groupcost : 0 ; 
				d.sumcost = d.cost * parseNum(d.num);
				d.sumrepaircost = d.repaircost * parseNum(d.num);
				sum.cost += d.cost;
				sum.num += parseNum(d.num);
				sum.repaircost += d.repaircost;
				sum.sumcost += d.sumcost;
				sum.sumrepaircost += d.sumrepaircost;
				res.push(d);

			}
			res.push(sum);
			return res; 
		}